﻿/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using EPiServer.Common;
using EPiServer.Research.Wiki.DynamicContent;
using EPiServer.Templates.RelatePlus.Templates.PageEntity;

namespace EPiServer.Research.Wiki.Units
{
    public partial class NumberOfViews : WikiElement
    {
        #region Private Member variables
        private PageEntity _currentPageEntity;
        #endregion

        #region Properties
        public PageEntity CurrentPageEntity
        {
            get
            {
                if ((_currentPageEntity == null && ViewState["currentPageEntityGUID"] != null))
                {
                    _currentPageEntity = PageEntityHandler.GetPageEntityByGUID((Guid)ViewState["currentPageEntityGUID"]);
                }

                if (_currentPageEntity == null)
                {
                    _currentPageEntity = PageEntityHandler.GetPageEntityByGUID(CurrentPage.PageGuid);
                }

                if (CurrentPage.PageTypeName != "[Wiki] Article")
                {
                    return null;
                }
                // Uncomment this if you are working with several community sites.
                // check that current site has been inititalized and defined
                //if (SiteHandler.CurrentSite == null)
                //{
                //    throw new EPiServerException("Current site is null - cannot create Page Entity");
                //}

                // if still null create it
                if (_currentPageEntity == null)
                {
                    _currentPageEntity = (PageEntity)Activator.CreateInstance(PageEntityUtils.GetPageEntityType(CurrentPage), CurrentPage.PageGuid,
                                                                  SiteHandler.CurrentSite);

                    _currentPageEntity = PageEntityHandler.AddPageEntity(_currentPageEntity);
                }
                return _currentPageEntity;
            }
            set
            {
                _currentPageEntity = value;
                ViewState["currentPageEntityGUID"] = (value != null) ? (object)value.PageGUID : null;
            }
        }

        /// <summary>
        /// Gets the number of views
        /// </summary>
        public string NumViews
        {
            get
            {
                if (CurrentPageEntity == null)
                {
                    return String.Empty;
                }
                return CurrentPageEntity.GetNumVisits().ToString();
            }
        }
        #endregion

        protected override string GetAscxUrl()
        {
            return "~/Templates/WikiX/Units/NumberOfViews.ascx";
        }
    }
}